package com.ybkj.o2o.zhuanxian.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ybkj.o2o.zhuanxian.common.DataGrid;
import com.ybkj.o2o.zhuanxian.entity.Car;
import com.ybkj.o2o.zhuanxian.entity.CarAndLine;
import com.ybkj.o2o.zhuanxian.entity.Line;
import com.ybkj.o2o.zhuanxian.manager.CarManager;
import com.ybkj.o2o.zhuanxian.service.CarAndLineService;
import com.ybkj.o2o.zhuanxian.service.CarService;
import com.ybkj.o2o.zhuanxian.service.LineService;

@Service
public class DefaultCarManager implements CarManager {

    @Autowired
    private CarService carService;
    @Autowired
    private CarAndLineService carAndLineService;
    @Autowired
    private LineService lineService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Car create(Car car) {
        if (car.getEmployId() != null) {
            Car carTemp = carService.findByEmploy(car.getEmployId(), car.getAppKey());
            if (carTemp != null) {
                carTemp.setEmployId(null);
                carTemp.setEmployName(null);
                carService.update(carTemp);
            }
        }
        if (car.getEmployId() != null) {
            Car cs = carService.findByEmploy(car.getEmployId(), car.getAppKey());
            if (cs != null && !cs.getId().equals(car.getId())) {
                carService.updateEmployIdToNull(cs);
            }
        }
        car = carService.create(car);

        if (car.getLines() != null && car.getLines().size() > 0) {
            List<Line> lines = car.getLines();
            for (Line line : lines) {
                CarAndLine cal = new CarAndLine();
                cal.setCarId(car.getId());
                cal.setCarName(car.getCarName());
                cal.setLineId(line.getId());
                cal.setLineName(line.getName());
                carAndLineService.insert(cal);
            }
        }
        return car;
    }

    @Override
    public Car findByEmploy(Long employId, String appKey) {
        Car car = carService.findByEmploy(employId, appKey);
        return car;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Car car) {
        //修改车辆名称
        carAndLineService.updateCarNameByCar(car.getId(), car.getCarName());
        //根据车辆id删除car_line表中的所有记录
        carAndLineService.deleteByCar(car.getId(), null);
        if (car.getLines() != null && car.getLines().size() > 0) {
            for (Line line : car.getLines()) {
                CarAndLine cal = new CarAndLine();
                cal.setCarId(car.getId());
                cal.setCarName(car.getCarName());
                cal.setLineId(line.getId());
                cal.setLineName(line.getName());
                carAndLineService.insert(cal);
            }
        }
        if (car.getEmployId() != null) {
            Car cs = carService.findByEmploy(car.getEmployId(), car.getAppKey());
            if (cs != null && !cs.getId().equals(car.getId())) {
                carService.updateEmployIdToNull(cs);
            }
        }
        if (StringUtils.isBlank(car.getEmployName())) {
            carService.update(car);
            carService.updateEmployIdToNullById(car);
        } else {
            carService.update(car);
        }


    }

    @Override
    public Car findOne(Long carId, String appKey) {
        Car car = carService.findOne(carId, appKey);
        List<CarAndLine> cal = carAndLineService.findByCar(carId);
        List<Long> lineIds = new ArrayList<Long>();
        for (CarAndLine c : cal) {
            lineIds.add(c.getLineId());
        }
        if (lineIds.size() > 0) {
            car.setLines(lineService.findByIds(lineIds));
        }
        return car;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long carId, Long[] lineId, String appKey) {
        //删除car_line表中的绑定数据
        for (int i = 0; i < lineId.length; i++) {
            carAndLineService.deleteByCar(carId, lineId[i]);
        }
        //删除car表中的车
        int rowcount = carService.deleteByIdAndAppKey(carId, appKey);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void delete(Long carId, String appKey) {
//        carAndLineService.deleteByCar(carId);
//        int rowcount = carService.deleteByIdAndAppKey(carId, appKey);
//    }

//  @Override
//  public DataGrid<Car> findByParam(Long lineId, String lineName, Long companyId,
//      String companyName, String carName, String carNumber, Integer[] status, Long employId,
//      String employName, Integer page, Integer rows, String appKey) {
//    Integer limit = null;
//    if (page != null && rows != null) {
//      limit = (page - 1) * rows;
//    }
//    List<Car> cars =
//        carService.findByParam(lineId, lineName, companyId, companyName, carName, carNumber,
//            status, employId, employName, limit, rows, appKey);
//    for (Car c : cars) {
//      List<CarAndLine> cals = carAndLineService.findByCar(c.getId());
//      List<Long> lineIds = new ArrayList<Long>();
//      /** 修改代码 */
////      lineIds.add(c.getLineId());
//      /**---------*/
//      for (CarAndLine cal : cals) {
//        lineIds.add(cal.getLineId());
//      }
//      if (lineIds.size() > 0) {
//        c.setLines(lineService.findByIds(lineIds));
//      }
//    }
//    Long count =
//        carService.countParam(lineId, lineName, companyId, companyName, carName, carNumber, status,
//            employId, employName, appKey);
//    DataGrid<Car> data = new DataGrid<Car>();
//    data.setRows(cars);
//    data.setTotal(count);
//    return data;
//  }

    /**
     * hgg
     */
    @Override
    public DataGrid<Car> findByParam(Long lineId, String lineName, Long companyId,
                                     String companyName, String carName, String carNumber, Integer[] status, Long employId,
                                     String employName, Integer page, Integer rows, String appKey) {
        Integer limit = null;
        if (page != null && rows != null) {
            limit = (page - 1) * rows;
        }
        List<Car> carList = null;
//    //根据线路参数查询车辆
//    if(StringUtils.isNotBlank(lineName)){
//        List<CarAndLine> calList = carAndLineService.findByLineName(lineName);
//        for(CarAndLine cal : calList){
//
//        }
//    }
        //查询car表中所有车辆
        carList = carService.findCarListByParams(lineName, companyId, companyName, carNumber, status, employId, employName, limit, rows, appKey);
        for (Car c : carList) {
            List<CarAndLine> cals = carAndLineService.findByCar(c.getId());
            List<Long> lineIds = new ArrayList<>();
            for (CarAndLine cal : cals) {
                lineIds.add(cal.getLineId());
            }
            if (lineIds.size() > 0) {
                c.setLines(lineService.findByIds(lineIds));
            }
        }
        Long count =
                carService.countParams(lineId, lineName, companyId, companyName, carName, carNumber, status,
                        employId, employName, appKey);
        DataGrid<Car> data = new DataGrid<Car>();
        data.setRows(carList);
        data.setTotal(count);
        return data;
    }

    @Override
    public List<Car> findBycarNumber(String carNumber, String appKey) {
        List<Car> list = carService.findBycarNumber(carNumber, appKey);
        for (Car car : list) {
            List<CarAndLine> cals = carAndLineService.findByCar(car.getId());
            List<Long> lineIds = new ArrayList<Long>();
            for (CarAndLine cal : cals) {
                lineIds.add(cal.getLineId());
            }
            if (lineIds.size() > 0) {
                car.setLines(lineService.findByIds(lineIds));
            }
        }
        return list;
    }

    @Override
    public Car queryCarByNumber(String carNumber, String appKey) {
        return carService.queryCarByNumber(carNumber, appKey);
    }

    @Override
    public Car findByNumber(String carNumber, String appKey) {
        return carService.findByNumber(carNumber, appKey);
    }

    @Override
    public int updateEmployIdToNull(Car car) {
        return carService.updateEmployIdToNull(car);
    }

}
